package handel

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"tea-egg/cache"
	"tea-egg/models"
	"tea-egg/pkg"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user models.User
	//判断
	redis_key := "redis_login" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount == 3 {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次,禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "此用户不存在",
		})
		return
	}
	if user.ID == 0 {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.MdeStr(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
		if errCount == 2 {
			c.JSONP(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次,禁止登录",
			})
			return
		}
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	c.JSONP(200, gin.H{
		"code": 500,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	if title == "" || content == "" {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Article{
		Title:   title,
		Content: content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var article []models.Article
	redis_key := "redis_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Offset(offset).Limit(size).Find(&article)
		jsonData, _ := json.Marshal(&article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &article)
	}
	var total int64
	models.DB.Model(&models.Article{}).Count(&total)
	c.JSONP(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  article,
		"total": total,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var article models.Article
	redis_key := "redis_list" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Where("id=?", id).Find(&article)
		jsonData, _ := json.Marshal(&article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &article)
	}

	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": article,
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")
	models.DB.Where("id=?", id).Delete(&models.Article{})
	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
